package com.example.imoocmusicdemo.activitys;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.imoocmusicdemo.R;
import com.example.imoocmusicdemo.adapters.MusicGridAdapter;
import com.example.imoocmusicdemo.adapters.MusicListAdapter;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity {


    private RecyclerView mRvGrid, mRvGrid2, mRvGrid3, mRvList;
    private MusicGridAdapter mGridAdapter;
    private ImageView more, iv1, iv2;
    private MusicListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        init();
        initBanner();
        moreOnClick();
        iv1OnClick();
        iv2OnClick();

    }

    private void iv2OnClick() {
        iv2 = fd(R.id.image2);
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MainActivity.this, MainActivity2.class);
                startActivity(i);
                finish();
            }
        });
    }

    private void iv1OnClick() {
        iv1 = fd(R.id.image1);
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "您已经在当前页面了", Toast.LENGTH_SHORT).show();
            }
        });
    }


    private void moreOnClick() {
        more = fd(R.id.iv_me);
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intentGet = getIntent();
                String number = intentGet.getStringExtra("number");
                if (number == null) {
                    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                    alertDialog.setIcon(R.drawable.question);
                    alertDialog.setTitle("    ");
                    alertDialog.setMessage("         是不是还没登录或注册过");
                    alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "登录", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "已为您跳转到登录页面", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(MainActivity.this, LoginActivity.class);
                            startActivity(i);
                            finish();
                        }
                    });
                    alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "注册", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "已为您跳转到注册页面", Toast.LENGTH_SHORT).show();
                            Intent i = new Intent(MainActivity.this, RegisterActivity.class);
                            startActivity(i);
                            finish();
                        }
                    });
                    alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(MainActivity.this, "你选择了取消", Toast.LENGTH_SHORT).show();
                        }
                    });
                    alertDialog.show();
                } else {
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.MoreActivity");
                    startActivity(intent);
                }
            }
        });
    }


    private void init() {
        initNavBar(false, "emo音乐", true);

        mRvGrid = fd(R.id.rv_grid);
        mRvGrid2 = fd(R.id.rv_grid2);
//        mRvGrid3 = fd(R.id.rv_grid3);
        mRvList = fd(R.id.rv_list);


        mRvGrid.setLayoutManager(new GridLayoutManager(this, 3));
        mRvGrid2.setLayoutManager(new GridLayoutManager(this, 3));
//        mRvGrid3.setLayoutManager(new GridLayoutManager(this, 3));
        mRvList.setLayoutManager(new LinearLayoutManager(this));

        mRvGrid.setNestedScrollingEnabled(false);
        mRvGrid2.setNestedScrollingEnabled(false);
//        mRvGrid3.setNestedScrollingEnabled(false);
        mRvList.setNestedScrollingEnabled(false);

        mGridAdapter = new MusicGridAdapter(this);
        mListAdapter = new MusicListAdapter(this);


        mRvGrid.setAdapter(mGridAdapter);
        mRvGrid2.setAdapter(mGridAdapter);
//        mRvGrid3.setAdapter(mGridAdapter);
        mRvList.setAdapter(mListAdapter);
    }


    //    轮播图
    public void initBanner() {
        List images = new ArrayList<>();
        images.add(R.drawable.banner);
        images.add(R.drawable.banner1);
        images.add(R.drawable.banner2);
        images.add(R.drawable.banner3);
        Banner banner = findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                Intent intent = new Intent();
                if (position == 1) {
                    intent.setAction("android.intent.action.PlayMusicActivity");
                    startActivity(intent);
                } else {
                    Toast.makeText(MainActivity.this, "你好", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}